package sdk.login.tvos.com.danmutest02;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.danmaku.loader.ILoader;
import master.flame.danmaku.danmaku.loader.IllegalDataException;
import master.flame.danmaku.danmaku.loader.android.DanmakuLoaderFactory;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDisplayer;
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.model.android.SimpleTextCacheStuffer;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.parser.IDataSource;
import master.flame.danmaku.danmaku.parser.android.BiliDanmukuParser;
import master.flame.danmaku.danmaku.util.IOUtils;

/**
 * Created by zhangxq on 16/3/2.
 */
public class DanmuControl {
    private Context mContext;
    private IDanmakuView mDanmakuView;      //显示弹幕的view
    private DanmakuContext mDanmakuContext; //配置的context
    private int scrollNum;              //显示的行数
    private BaseDanmakuParser mParser;  //解析器对象
    private final Paint paint;

    public DanmuControl(Context context, IDanmakuView danmakuView) {
        this.mContext = context;
        this.mDanmakuView = danmakuView;
        danmakuView.showFPS(true);
        scrollNum = 3;
        initDanmuConfig();
        paint = new Paint();

    }

    /**
     * 初始化配置
     */
    private void initDanmuConfig() {
        // 设置最大显示行数
        HashMap<Integer, Integer> maxLinesPair = new HashMap<Integer, Integer>();
        maxLinesPair.put(BaseDanmaku.TYPE_SCROLL_RL, scrollNum); // 滚动弹幕最大显示3行
        // 设置是否禁止重叠
        HashMap<Integer, Boolean> overlappingEnablePair = new HashMap<Integer, Boolean>();
        overlappingEnablePair.put(BaseDanmaku.TYPE_SCROLL_RL, true);
        overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_BOTTOM, true);

        mDanmakuContext = DanmakuContext.create();
        mDanmakuContext
                .setDanmakuStyle(IDisplayer.DANMAKU_STYLE_NONE)
                .setDuplicateMergingEnabled(false)
                .setScrollSpeedFactor(1.2f)//越大速度越慢
                .setScaleTextSize(1.2f)    //文字时候缩放
                .setCacheStuffer(new MyCacheStuffer(mContext), mCacheStufferAdapter) //自定义view 画背景
                .setMaximumLines(maxLinesPair)
                .preventOverlapping(overlappingEnablePair);

        if (mDanmakuView != null) {
            mDanmakuView.setCallback(new DrawHandler.Callback() {
                @Override
                public void prepared() {
                    mDanmakuView.start();
                }

                @Override
                public void updateTimer(DanmakuTimer timer) {
                }

                @Override
                public void danmakuShown(BaseDanmaku danmaku) {
                }

                @Override
                public void drawingFinished() {
                }
            });
        }

        //直播解析器
        mParser = new BaseDanmakuParser() {
            @Override
            protected Danmakus parse() {
                return new Danmakus();
            }
        };


        mDanmakuView.enableDanmakuDrawingCache(true);
    }

    private BaseCacheStuffer.Proxy mCacheStufferAdapter = new BaseCacheStuffer.Proxy() {

        @Override
        public void prepareDrawing(final BaseDanmaku danmaku, boolean fromWorkerThread) {
        }

        @Override
        public void releaseResource(BaseDanmaku danmaku) {
            // tag包含bitmap，一定要清空
            danmaku.tag = null;
        }
    };

    /**
     * 添加弹幕
     *
     * @param avatorUrl 图片URL
     * @param content   弹幕内容
     */
    public void addDanmu(final String avatorUrl, final String name, final String content) {
        if(!mDanmakuView.isPrepared()){
            return;
        }
        final BaseDanmaku danmaku = mDanmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);

        new Thread() {
            @Override
            public void run() {
                InputStream inputStream = null;
                try {
                    // 从网络获取图片并且保存到一个bitmap里
                    URLConnection urlConnection = new URL(avatorUrl).openConnection();
                    inputStream = urlConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    bitmap = toCircleBitmap(bitmap);
                    // 组装需要传递给danmaku的数据
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("content", content);
                    map.put("bitmap", bitmap);
                    map.put("name",name);
                    danmaku.tag = map;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
                danmaku.text = "";
                danmaku.padding = 0;
                danmaku.priority = 0;  //  1一定会显示, 一般用于本机发送的弹幕
                danmaku.isLive = true;
                danmaku.setTime(mDanmakuView.getCurrentTime());//=mDanmakuView.getCurrentTime();// +1000;//立即显示
                danmaku.textSize = 0;
                mDanmakuView.addDanmaku(danmaku);
            }
        }.start();
    }


    /**
     * 解析本地文件
     * 本地评论作为弹幕  //创建解析器对象，从raw资源目录下解析comments.xml文本
     */
    public void parserLocation(InputStream stream) {
        mDanmakuContext.setCacheStuffer(new SimpleTextCacheStuffer(),null);
        ILoader loader = DanmakuLoaderFactory.create(DanmakuLoaderFactory.TAG_BILI);
        try {
            loader.load(stream);
        } catch (IllegalDataException e) {
            e.printStackTrace();
        }
        mParser = null;
        mParser = new BiliDanmukuParser();
        IDataSource<?> dataSource = loader.getDataSource();
        mParser.load(dataSource);
    }





    /**
     * 开始滚动弹幕
     */
    public void startDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null && !mDanmakuView.isPrepared()) {
            mDanmakuView.prepare(mParser, mDanmakuContext);
        }
    }

    /**
     * 暂停
     */
    public void pauseDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null && mDanmakuView.isPrepared()) {
            mDanmakuView.pause();
        }
    }

    /**
     * 继续
     */
    public void resumeDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null && mDanmakuView.isPrepared()) {
            mDanmakuView.resume();
        }
    }

    /**
     * 隐藏
     */
    public void hideDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null && mDanmakuView.isShown()) {
            mDanmakuView.hide();
        }
    }

    /**
     * 显示
     */
    public void showDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null) {
            mDanmakuView.show();
        }
    }

    /**
     * 结束
     */
    public void stopDanMu() {
        if (mDanmakuView != null && mParser != null && mDanmakuContext != null) {
            mDanmakuView.release();
        }
    }


    /**
     * 将图片变成圆形
     *
     * @param bitmap
     * @return
     */
    private static Bitmap makeRoundCorner(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int left = 0, top = 0, right = width, bottom = height;
        float roundPx = height / 2;
        if (width > height) {
            left = (width - height) / 2;
            top = 0;
            right = left + height;
            bottom = height;
        } else if (height > width) {
            left = 0;
            top = (height - width) / 2;
            right = width;
            bottom = top + width;
            roundPx = width / 2;
        }
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(output);
        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(left, top, right, bottom);
        RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }


    /**
     * 圆形图
     * @param bitmap
     * @return
     */
    public static Bitmap toCircleBitmap(Bitmap bitmap) {
        int min = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getHeight() : bitmap.getWidth();
        int max = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() : bitmap.getHeight();
        Bitmap output = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Paint paint = new Paint();
        RectF rectF = new RectF(0, 0, min, min);
        canvas.drawOval(rectF, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        if (min == bitmap.getWidth()) {
            canvas.drawBitmap(bitmap, 0, (min - max) / 2, paint);
        } else {
            canvas.drawBitmap(bitmap, (min - max) / 2, 0, paint);
        }
        return output;
    }

}
